<?php

namespace Chap11Bundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;

class DefaultController extends Controller
{
    /**
     * @Route("/", name="chap11_default_index")
     */
    public function indexAction()
    {
        return $this->render('Chap11Bundle:Default:index.html.twig');
    }

    /**
     * @Route("/post/show/{slug}")
     * @param $slug
     * @return Response
     */
    public function showPostAction($slug)
    {
        return new Response($slug);
    }


    /**
     * @Route("/post/create", name="chap11_default_postcreate")
     */
    public function createPostAction(Request $request)
    {

        if ($request->server->get('REQUEST_METHOD') == 'POST') {

            $username = $request->request->get('username');
            $mobile = $request->request->get('mobile');

            $data = array('username'=>$username, 'mobile'=>$mobile);

            return new Response(json_encode($data), 200, array('Content-type'=>"application/json"));
        }

        return $this->render('Chap11Bundle:Default:postcreate.html.twig');
    }





}
